﻿using Guoling.ProjectName.Application;
using Guoling.ProjectName.Model;
using Guoling.ProjectName.Web.Models;
using Microsoft.AspNetCore.Mvc;

namespace Guoling.ProjectName.Web.Controllers
{
    [ApiController]
    [Route("api/v1/[controller]")]
    public class UserController : BaseController
    {
        private readonly IUserAppService userAppService;

        public UserController(UserService user, IUserAppService userAppService) : base(user)
        {
            this.userAppService = userAppService;
        }

        /// <summary>
        /// 添加人员
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> Add([FromBody] UseAddModel model)
        {
            //校验:todo
            var result = await userAppService.Add(new UserViewModel
            {
                Name = model.Name,
                Gender = model.Gender,
                Tel = model.Tel
            });
            if (result) return Ok();
            return StatusCode(500, "添加失败");
        }
        /// <summary>
        /// 获取人员
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> Get()
        {
            return await userAppService.GetAll();
        }
        [HttpPut]
        public async Task<object> Update([FromBody] UserUpdateModel model)
        {
            return await userAppService.Update(new UserViewModel
            {
                Id = model.Id,
                Name = model.Name,
                Gender = model.Gender,
                Tel = model.Tel
            });
        }
    }
}
